﻿
using CommunityToolkit.Mvvm.ComponentModel;
using Company.Core.Enums;
using Company.Core.Interfaces;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using System.Collections.ObjectModel;

namespace Company.Shell.Services
{
    public class ConfigProvider : ObservableObject, IConfigProvider
    {
        private Config config = new();
        /// <summary>
        /// 参数
        /// </summary>
        public Config Config
        {
            get => config;
            set => SetProperty(ref config, value);
        }

        private ObservableCollection<AlarmItem> alarmItems = new();
        /// <summary>
        /// 报警内容描述
        /// </summary>
        public ObservableCollection<AlarmItem> AlarmItems
        {
            get => alarmItems;
            set => SetProperty(ref alarmItems, value);
        }

        public ConfigProvider(IConfigManager configManager)
        {
            var cfg = configManager.Read<Config>(ConfigType.System);//读取系统配置
            if (cfg != null)
            {
                Config = cfg;
            }
            else
            {
                configManager.Write(ConfigType.System, Config);//写入默认配置
            }
            var cfg2 = configManager.Read<ObservableCollection<AlarmItem>>(ConfigType.Alarm);//读取报警配置
            if (cfg2 != null)
            {
                AlarmItems = cfg2;
            }
            else
            {
                configManager.Write(ConfigType.Alarm, AlarmItems);//写入默认配置
            }


        }
    }
}
